﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Input;
using MEF_Test.Bricks;
using MEF_Test.Bricks.TemplateProvider;
using MEF_Test.Contracts;
using MEF_Test.Module.AudioStation.Properties;
using Microsoft.Practices.Unity;
using Synology.AudioStationApi;


namespace MEF_Test.Module.AudioStation
{
    /// <summary>
    /// Represents and contains all the information to issue a Query on AudioStation and get its "subnodes" !
    /// </summary>
    public class AudioStationMainViewModel
    {
        public AudioStationMainViewModel()
        {            
            StartSearch = new ParameterlessCommand(OnExecuteMethod, ()=>IsSearchPending);
            var fh = new AudioStationFileHandler(AudioStationAction.Browse, new SynoItem { ItemID = "musiclib_root", ItemPid = "musiclib_root" , IsContainer = true,IsTrack = false,Sequence = 0,Title = "Categories"});

            /* FIXME : Replace following INSTANCIATION WITH A FACTORY that knows which TemplateProvider to use
            * (since the filehandler is passed as an argument, it is possible to know the kind of node,
            * and therefore, which template to apply to it, but not by asking for the template to the filehandler !
            * rather having a heuristic based on the existing infos found in the filehandler.*/
            var templateProvider = IoC.Current.Resolve<TemplateProvider>();
            var audioStationBrowserNodeViewModelFactory = new AudioStationBrowserNodeViewModelFactory(templateProvider);
            var browserNodeViewModel = audioStationBrowserNodeViewModelFactory.Create(fh);

            BrowsingProvider = new AudioStationBrowsingProvider(browserNodeViewModel, Settings.Default.SynoHost,Settings.Default.SynoPort);
            BrowsingProvider.InvalidateChildren();
        }

        protected bool IsSearchPending { get; set; }

        private void OnExecuteMethod()
        {
            var syno = new AudioStationSession((string)Settings.Default["SynoHost"], (int)Settings.Default["SynoPort"],(string)Settings.Default["Login"] , (string)Settings.Default["Password"]);

            IsSearchPending = true;

            syno.LoginAsync((string) Settings.Default["Login"], (string) Settings.Default["Pasword"]);
            


        }

        public IEnumerable BrowserElements { get; set; }

        public ICommand StartSearch { get; set; }

        public AudioStationBrowsingProvider BrowsingProvider { get; set; }
    }

    public enum AudioStationAction
    {
        Search,
        Browse
    }
}